package com.sun.mybatisplus.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

/**
 * @author syh
 * @date 2021/9/28 13:44
 */
@RestController
public class CountController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @RequestMapping("/hello")
    public String count() {
        Long count = stringRedisTemplate.opsForValue().increment("count");
        return "访问了" + count + "次";
    }


    @RequestMapping("/set")
    public String set() {
        Long user001 = stringRedisTemplate.opsForSet().add("act:1000", "user001");
        Long user002 = stringRedisTemplate.opsForSet().add("act:1000", "user002");
        Long user003 = stringRedisTemplate.opsForSet().add("act:1000", "user003");
        Set<String> members = stringRedisTemplate.opsForSet().members("act:1000");
        return "参与抽奖的人" + members;
    }

    @RequestMapping("/get")
    public String get() {
        //获取到的人不会在原来的数据中删除
        Set<String> members = stringRedisTemplate.opsForSet().distinctRandomMembers("act:1000", 2);
        //获取到的人会在原来的数据中删除(抽完一等奖的人不在继续抽取二等奖)
       // stringRedisTemplate.opsForSet().pop("act:1000",2);

        //点赞
        stringRedisTemplate.opsForSet().add("messageId","userId001");
        stringRedisTemplate.opsForSet().add("messageId","userId002");
        stringRedisTemplate.opsForSet().add("messageId","userId003");

        //userId001取消点赞
        stringRedisTemplate.opsForSet().remove("messageId","userId001");
        //检查用户userId002是否点过赞
        Boolean aBoolean = stringRedisTemplate.opsForSet().isMember("messageId", "userId002");
        //获取点赞的用户列表
        Set<String> messageId = stringRedisTemplate.opsForSet().members("messageId");
        //获取点赞的用户数
        Long count = stringRedisTemplate.opsForSet().size("messageId");
        //return "中奖的人为：" + members;

        return "点赞人数：" + count;
    }
}

